package com.ttt.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ttt.pojo.Course;
import com.ttt.service.SelectCourseCenterService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SelectCourseCenterServlet")
public class SelectCourseCenterController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        resp.setContentType("test/html,utf-8");

        //获取前端查询条件


        SelectCourseCenterService courseCenterService = new SelectCourseCenterService();
        try {
            //展示全部课程
            List<Course> course = courseCenterService.findCourse();
            //转JSON
            Map<String,Object> responseData = new HashMap<>();
            responseData.put("courses",course);

            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(responseData);
            // 设置响应头
            resp.setContentType("application/json");
            // 输出响应
            PrintWriter out = resp.getWriter();
            out.print(json);

            //按条件查询课程


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
